package com.lanou3g.web;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanou3g.bean.Compony;
import com.lanou3g.bean.Flats;
import com.lanou3g.bean.Person;
import com.lanou3g.service.ComponyService;
import com.lanou3g.service.FlatsService;
import com.lanou3g.service.PersonService;
import com.lanou3g.util.Result;

@Controller
@RequestMapping("/person")
public class PersonController {

	@Autowired
	private PersonService personService;

	@Autowired
	private ComponyService componyService;

	@Autowired
	private FlatsService flatsService;

	// 进入人员列表页面
	@RequestMapping("/personList")
	public String personList(HttpSession session) {
		// 获取公司列表
		List<Compony> componyList = componyService.getComponies();
		session.setAttribute("componies", componyList);
		// 获取宿舍列表
		List<Flats> flats = flatsService.getFlats();
		session.setAttribute("flats", flats);
		return "person/personList";
	}

	// 获取人员列表数据
	@ResponseBody
	@RequestMapping("/getAllPersons")
	public Result getAllPersons(Integer page, Integer limit,Person person) {
		System.out.println("getAllPersons: page = " + page + ",limit = " + limit);
		System.out.println(person);
		return personService.getAllPersons(page, limit, person);
	}

	// 进入添加人员界面
	@RequestMapping("/addPerson")
	public String addPerson(HttpSession session) {
		// 获取公司列表
		List<Compony> componyList = componyService.getComponies();
		session.setAttribute("componies", componyList);
		// 获取宿舍列表
		List<Flats> flats = flatsService.getFlats();
		session.setAttribute("flats", flats);
		return "person/addPerson";
	}

	// 查询人员是否存在
	@ResponseBody
	@RequestMapping("/checkPersonByName/{name}")
	public Result checkPersonByName(@PathVariable("name") String name) {
		return personService.checkPersonByName(name);
	}

	// 添加人员
	@ResponseBody
	@RequestMapping("/insertPerson")
	public Result insertPerson(Person person) {
		return personService.insertPerson(person);
	}

	// 进入人员编辑页面
	@RequestMapping("/editPerson/{id}")
	public String editPerson(@PathVariable("id") Integer id, HttpSession session) {
		// 根据id获取人员信息
		Person person = personService.getPersonById(id);
		session.setAttribute("editPerson", person);
		// 获取公司信息
		List<Compony> componies = componyService.getComponies();
		session.setAttribute("componies", componies);
		// 获取宿舍列表
		List<Flats> flats = flatsService.getFlats();
		session.setAttribute("flats", flats);
		return "person/editPerson";
	}

	// 更新人员信息
	@ResponseBody
	@RequestMapping("/updatePerson")
	public Result updatePerson(Person person) {
		return personService.updatePerson(person);
	}

	// 根据id删除人员
	@ResponseBody
	@RequestMapping("/deletePersonById/{id}")
	public Result deletePersonById(@PathVariable("id") Integer id) {
		return personService.deletePersonById(id);
	}
}
